//    uniCenta oPOS  - Touch Friendly Point Of Sale
//    Copyright (c) 2009-2013 uniCenta & previous Openbravo POS works
//    http://www.unicenta.com
//
//    This file is part of uniCenta oPOS
//
//    uniCenta oPOS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//   uniCenta oPOS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with uniCenta oPOS.  If not, see <http://www.gnu.org/licenses/>.

package com.openbravo.pos.epm;

import com.openbravo.basic.BasicException;
import com.openbravo.data.loader.SentenceList;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.data.user.EditorRecord;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.AppView;
import java.awt.Component;
import java.util.List;
import java.util.UUID;

/**
 *
 * @author Ali Safdar & Aneeqa Baber
 */
public final class BreaksView extends javax.swing.JPanel implements EditorRecord {

    private Object m_oId;
    private SentenceList m_sentcat;
    private DirtyManager m_Dirty;

    /** Creates new form BreaksView */
    public BreaksView(AppView app, DirtyManager dirty) {

        DataLogicPresenceManagement dlPresenceManagement = (DataLogicPresenceManagement) app.getBean("com.openbravo.pos.epm.DataLogicPresenceManagement");
        initComponents();
        m_sentcat = dlPresenceManagement.getBreaksList();
        m_Dirty = dirty;
        m_jBreakName.getDocument().addDocumentListener(dirty);
        m_jVisible.addActionListener(dirty);
        m_jBreakDescription.getDocument().addDocumentListener(dirty);
        writeValueEOF();
    }

    void activate() throws BasicException {
        List a = m_sentcat.list();
        a.add(0, null);
    }

    @Override
    public void writeValueEOF() {
        m_oId = null;
        m_jBreakName.setText(null);
        m_jBreakDescription.setText(null);
        m_jVisible.setSelected(false);
        m_jBreakName.setEditable(false);
        m_jBreakDescription.setEnabled(false);
        m_jVisible.setEnabled(false);
    }

    @Override
    public void writeValueInsert() {
        m_oId = null;
        m_jBreakName.setText(null);
        m_jBreakDescription.setText(null);
        m_jVisible.setSelected(true);
        m_jBreakName.setEditable(true);
        m_jBreakDescription.setEnabled(true);
        m_jVisible.setEnabled(true);
    }

    @Override
    public void writeValueEdit(Object value) {
        Object[] breaks = (Object[]) value;
        m_oId = breaks[0];
        m_jBreakName.setText((String) breaks[1]);
        m_jBreakDescription.setText((String) breaks[2]);
        m_jVisible.setSelected(((Boolean) breaks[3]).booleanValue());
        m_jBreakName.setEditable(true);
        m_jBreakDescription.setEnabled(true);
        m_jVisible.setEnabled(true);
    }

    @Override
    public void writeValueDelete(Object value) {
        Object[] breaks = (Object[]) value;
        m_oId = breaks[0];
        m_jBreakName.setText((String) breaks[1]);
        m_jBreakDescription.setText((String) breaks[2]);
        m_jVisible.setSelected(((Boolean) breaks[3]).booleanValue());
        m_jBreakName.setEditable(false);
        m_jBreakDescription.setEnabled(false);
        m_jVisible.setEnabled(false);
    }

    @Override
    public void refresh() {
    }

    @Override
    public Component getComponent() {
        return this;
    }

    @Override
    public Object createValue() throws BasicException {
        Object[] breaks = new Object[4];
        breaks[0] = m_oId == null ? UUID.randomUUID().toString() : m_oId;
        breaks[1] = m_jBreakName.getText();
        breaks[2] = m_jBreakDescription.getText();
        breaks[3] = Boolean.valueOf(m_jVisible.isSelected());

        return breaks;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        m_jBreakName = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        m_jBreakDescription = new javax.swing.JTextArea();
        m_jVisible = new javax.swing.JCheckBox();
        m_Ncx = new javax.swing.JLabel();
        m_Name3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        m_jBreakName.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        m_jBreakDescription.setColumns(20);
        m_jBreakDescription.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        m_jBreakDescription.setLineWrap(true);
        m_jBreakDescription.setRows(5);
        jScrollPane1.setViewportView(m_jBreakDescription);

        m_jVisible.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        m_Ncx.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        m_Ncx.setText(AppLocal.getIntString("label.epm.visible")); // NOI18N

        m_Name3.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        m_Name3.setText(AppLocal.getIntString("label.epm.notes")); // NOI18N

        jLabel1.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("pos_messages"); // NOI18N
        jLabel1.setText(bundle.getString("label.epm.employee")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                    .addComponent(m_Name3, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                    .addComponent(m_Ncx, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(m_jVisible)
                    .addComponent(m_jBreakName)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(214, 214, 214))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(m_jBreakName, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(m_Ncx, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(m_Name3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(m_jVisible, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(35, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel m_Name3;
    private javax.swing.JLabel m_Ncx;
    private javax.swing.JTextArea m_jBreakDescription;
    private javax.swing.JTextField m_jBreakName;
    private javax.swing.JCheckBox m_jVisible;
    // End of variables declaration//GEN-END:variables
}
